<?php
namespace app\service;

use think\Log;

class ShellRunnerService{

    public static function getCommandOfRunBackground($command){
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
           return self::getCommandRunBackgroundByWindow($command);
        }else{
            return self::getCommandRunBackgroundByLinux($command);
        }
    }

    /**
     * windows 的 nohup  有点问题
     * 返回示例 nohup php F:\data\wwwroot\enz-api-rebuild\account\cli.php CMqBack sleep 2>&1 &
     * @param string  $command
     * @return string
     */
    public static function getCommandRunBackgroundByWindow($command){
        $formatCommand = sprintf('nohup php %s  2>&1 &',$command);
        trace("try to run shell command ".$formatCommand, Log::ERROR);
        return $formatCommand;
    }

    /**
     * 返回示例 nohup php /var/www/enz-api/account/cli.php CMqBack sleep > /dev/null 2 > /dev/null 2>&1  &
     * @param string 
     * @return string
     * 
     */
    public static function getCommandRunBackgroundByLinux($command){
        $formatCommand = sprintf('nohup php %s > /dev/null 2 > /dev/null 2>&1 &',$command);
        trace("try to run shell command ".$formatCommand, Log::ERROR);
        return $formatCommand;
    }


}